﻿using LJ.Abstractions.DependencyInjection;
using LJ.EventBus.HornedSerpent.Transport.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJ.EventBus.HornedSerpent.Transport.InMemoryQueue.Internal;


public class InMemoryEventBusQueueConsumerClientFactory(IDependencyInjectionFactory dependencyInjectionFactory) : IEventBusConsumerClientFactory
{
    private readonly IDependencyInjectionFactory _dependencyInjectionFactory = dependencyInjectionFactory;

    public IEventBusConsumerClient Create(string group, string topic)
    {
        using var serviceScope = _dependencyInjectionFactory.CreateScope();
        var serviceProvider = serviceScope.ServiceProvider;

        var consumerClient = serviceProvider.GetRequiredService<IEventBusConsumerClient>();

        if (consumerClient is not InMemoryEventBusQueueConsumerClient inMemoryQueueConsumerClient)
        {
            throw new ArgumentException($"{GetType().FullName} IConsumerClient is not {nameof(InMemoryEventBusQueueConsumerClient)}");
        }
        inMemoryQueueConsumerClient.Init(group, topic);
        return inMemoryQueueConsumerClient;
    }
}